package gae.google.tcserver.servlets;


import gae.google.tcserver.model.CalendarBean;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The <tt>CalendarServlet</tt> class creates a calendar from
 * received day and month. 
 *
 * @author Lars Friberg
 *
 * @date 2011-02-04
 */
public class CalendarServlet extends HttpServlet {

	/**
	 * Serial version Id
	 */
	private static final long serialVersionUID = -7145960591762356266L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int iYear = 0;
		int iMonth = 0;
	 	int iTYear = 0;
	 	int iTMonth = 0;
	 	int days = 0;
	 	int weekStartDay = 0;

		String year = null;
		String month = null;
		
		Calendar ca;
		GregorianCalendar cal;
		DateFormat formatter;
		RequestDispatcher view;
		
		// get received  parameters 
		year = request.getParameter("year");
		month = request.getParameter("month");
		
		CalendarBean calendarBean = new CalendarBean();
		
		if (year != null && month != null){
			if (!year.isEmpty() && !month.isEmpty()){
				iYear = Integer.valueOf(year);
				iMonth = Integer.valueOf(month);
			}
		}
		
	 	ca = new GregorianCalendar();
	 	iTYear = ca.get(Calendar.YEAR);
	 	iTMonth = ca.get(Calendar.MONTH);

	 	if (iYear == 0){
		  	iYear = iTYear;
		  	iMonth = iTMonth;
	 	}
		cal = new GregorianCalendar (iYear, iMonth, 1); 
	 	cal.setMinimalDaysInFirstWeek(1);
	 	cal.setFirstDayOfWeek(Calendar.MONDAY);
	 	calendarBean.setWeekYear(cal.get(Calendar.WEEK_OF_YEAR));

	 	formatter = new SimpleDateFormat("MMMMM", new java.util.Locale("se", "SE"));
	    cal.set(Calendar.MONTH, iMonth);
	    month = formatter.format(cal.getTime());
	    calendarBean.setMonthName(month);
	    calendarBean.setMonth(iMonth);
	    calendarBean.setYear(iYear);
		
	    days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	 	calendarBean.setNoOfDaysInMonth(days);
	 	weekStartDay = cal.get(Calendar.DAY_OF_WEEK);
	 	if (weekStartDay == 1){
			weekStartDay = 7;
	 	}else {
		 	weekStartDay -= 1;
	 	}
	 	calendarBean.setWeekStartDay(weekStartDay);
	 	cal = new GregorianCalendar (iYear, iMonth, days); 
	 	calendarBean.setTotalWeeksInMonth(cal.get(Calendar.WEEK_OF_MONTH));
		
	 	request.setAttribute("calendar", calendarBean);
	 	
		// dispatch to Calendar.jsp
		view = getServletContext().getRequestDispatcher("/views/Calendar.jsp");
		view.forward(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException
	{
		// dispatch to Poll.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/calendar.jsp");
		view.forward(request, response);
	}
}